package com.chinaums.xm.wechat.commons;

import java.util.Arrays;
import java.util.Optional;

import com.chinaums.xm.wechat.WeixinConstant;

/**
 * 签名验签工具
 * @author chenjf
 *
 */
public class CheckUtil {

	public static boolean checkSignature(final String signature, final String timestamp, final String nonce) {
		String arr[] = new String[] { WeixinConstant.TOKEN, timestamp, nonce };
		Arrays.sort(arr);
		StringBuffer content = new StringBuffer();
		for (int i = 0; i < arr.length; i++) {
			content.append(arr[i]);
		}
		Optional<String> sha = ShaUtil.getSha1(String.valueOf(content));
		if (sha.isPresent()) {
			return sha.get().equals(signature);
		}
		return false;
	}
}
